<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<script src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
	<script src="/js/jq.js"></script>
	<script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script src="/js/common.js"></script>
	<script src="/layui/layui.js"></script>
	<script src="/js/dict.js"></script>
	<script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script src="/js/my/ztree-menu.js"></script>
	<script src="/js/my/permission.js"></script>
	<script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
	<script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<div class='form-group'>
				<label class='col-md-4 control-label'>商品名称</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='商品名称' type='text' name='name' id='name'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>编码</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='编码' type='text' name='code' id='code'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>商品类型</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='category' id='category' >
					<input style="width: 90%;float: left;" class='form-control' placeholder='商品类型' type='text' name='categoryName' id='categoryName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTree('category', 'categoryName', 'goodsCategory', '商品类型', 'root', 'select')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>规格</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='规格' type='text' name='specification' id='specification'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>单位</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='单位' type='text' name='unit' id='unit'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>单价</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='单价' type='text' name='unitPrice' id='unitPrice'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否常规物料</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isCommon" id="isCommon"
							title="是否常规物料" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否耗材</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isConsumables" id="isConsumables"
							title="是否耗材" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>备注</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='备注' type='text' name='remark' id='remark'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-2 control-label'>附件</label>
				<div class="layui-upload col-md-10">
					<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="selectScreenshotPath">选择多文件</button>
					<input type="hidden" value="" id="screenshotPath" name="screenshotPath">
					<button type="button" class="layui-btn layui-btn-sm" id="uploadScreenshotPath">开始上传</button>
					<div class="layui-upload-list">
						<table class="layui-table">
							<thead>
							<tr>
								<th>文件名</th>
								<th>大小</th>
								<th>状态</th>
								<th>操作</th>
							</tr>
							</thead>
							<tbody id="screenshotPathList"></tbody>
						</table>
					</div>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>
		</fieldset>
	</form>
</div>
<script type="text/javascript">
    layui.use(['layer','laydate','upload'], function(){
        var layer = layui.layer;
		var upload = layui.upload;

		var screenshotPathListView = $('#screenshotPathList');
		var uploadListIns = upload.render({
			elem: '#selectScreenshotPath'
			,url: '/files'
			,accept: 'file'
			,multiple: true
			,auto: false
			,bindAction: '#uploadScreenshotPath'
			,choose: function(obj){
				var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
				//读取本地文件
				obj.preview(function(index, file, result){
					var tr = $(['<tr id="upload-'+ index +'">'
						,'<td>'+ file.name +'</td>'
						,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
						,'<td>等待上传</td>'
						,'<td>'
						,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
						,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
						,'</td>'
						,'</tr>'].join(''));

					//单个重传
					tr.find('.demo-reload').on('click', function(){
						obj.upload(index, file);
					});

					//删除
					tr.find('.demo-delete').on('click', function(){
						delete files[index]; //删除对应的文件
						tr.remove();
						// 重新获得所有上传文件
						$('#screenshotPath').val('');
						$('.screenshotPathFile').each(function () {
							$('#screenshotPath').val($('#screenshotPath').val() + '|' + $(this).data('id'));
						});
						uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
					});

					screenshotPathListView.append(tr);
				});
			}
			,done: function(res, index, upload){
				if(res.code === 0){ //上传成功
					var tr = screenshotPathListView.find('tr#upload-'+ index)
							,tds = tr.children();
					tds.eq(0).html('<a target="_blank" data-id="' + res.data.id + '" class="screenshotPathFile" href="/files' + res.data.url + '">' + res.data.fileName + '</a>');
					tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
					// tds.eq(3).html(''); //清空操作

					// 追加文件id
					$('#screenshotPath').val($('#screenshotPath').val() + '|' + res.data.id);
					return delete this.files[index]; //删除文件队列已经上传成功的文件
				}
				this.error(index, upload);
			}
			,error: function(index, upload){
				var tr = screenshotPathListView.find('tr#upload-'+ index)
						,tds = tr.children();
				tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
				tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
			}
		});
    });

    // 设置验证规则
    var validatorSettings = {
        message: '验证不通过',
        fields: {
            name: {
                message: '商品名称验证失败',
                validators: {
                    notEmpty: {
                        message: '商品名称不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 64,
                        message: '商品名称长度必须在1到64位之间'
                    }
                }
            },
            code: {
                message: '编码验证失败',
                validators: {
                    notEmpty: {
                        message: '编码不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 64,
                        message: '编码长度必须在1到64位之间'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_-]+$/,
                        message: '编码只能包含大写、小写、数字、破折号和下划线'
                    }
                }
            },
            categoryName: {
                message: '商品类型验证失败',
                validators: {
                    notEmpty: {
                        message: '商品类型不能为空'
                    }
                }
            }
        },
        submitHandler: function (validator, form, submitButton) {
            alert("submit");
        }
    };

    $('#form').bootstrapValidator(validatorSettings);

    showDictSelect('isCommon', 'boolean', '', '1');
    showDictSelect('isConsumables', 'boolean', '', '0');

    function save() {
        $('#form').bootstrapValidator(validatorSettings);
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
            return;
        }

        var formdata = $("#form").serializeObject();

        $.ajax({
            type : 'post',
            url : '/goods/',
            contentType: "application/json; charset=utf-8",
            data : JSON.stringify(formdata),
            success : function(json) {
                if (json.code === 0) {
                    layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                        parent.location.reload();//关闭iFrame
                    });
                } else {
                    layer.msg("添加失败：" + json.message, {shift: -1, time: 1000});
                }
            }
        });
    }

</script>
</body>
</html>
